export function setLocation(name: string, val:string) {
    const content = window.localStorage.getItem(name)
    if (content) {
        let arr = convertArr(content)
        if (arr.find(el => el === val)) {
            return
        }
        arr.unshift(val)
        console.log(arr);
        window.localStorage.setItem(name, JSON.stringify(arr))
        return
    } 
    let arr: string[] = []
    arr.push(val)
    window.localStorage.setItem(name, JSON.stringify(arr))
}
export function getLocation(name: string): string[] | null{
    const val = window.localStorage.getItem(name)
    if (val) {
        return convertArr(val)
    } 
    return null
}
export function delLocation(name: string, val:string) {
    const content = convertArr(window.localStorage.getItem(name) as string)
    const index = content.findIndex(el => el === val)
    content.splice(index, 1)
    window.localStorage.setItem(name, JSON.stringify(content))
}
export function clearByName(name: string) {
    window.localStorage.removeItem(name)
}
function convertArr(val:string): string[] {
    return JSON.parse(val, (key, value) => {
        if (Array.isArray(value)) {
            return value
        }
        return value
    })
}